package autokeyboard.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractListModel;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import autokeyboard.handler.HelpDialogEvent;

@SuppressWarnings({ "serial", "rawtypes", "unchecked"})
public class HelpDialog extends JDialog
{

	private JScrollPane	scrollList	= new JScrollPane();
	private JScrollPane	scrollIntro	= new JScrollPane();
	private JTextArea	textArea	= new JTextArea();
	private JList		list		= new JList();
	
	private HelpDialogEvent event = new HelpDialogEvent();

	public HelpDialog(JFrame frame)
	{
		super(frame, true);
		initComponent();
		this.setTitle("Help");
		this.setPreferredSize(new Dimension(600, 400));
		this.setMinimumSize(new Dimension(600, 400));
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setIconImage(new ImageIcon(getClass().getResource("/Small_Icons/Icon.png")).getImage());
		this.setResizable(false);
		this.setLocationRelativeTo(frame);
	}

	private void initComponent()
	{
		setComponentProperties();
		setMainLayout();
		
		keyEvent();
		setListEvent();
		event.showGameIntro(textArea);
	}

	private void setMainLayout()
	{
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(scrollList);
		this.add(scrollIntro);
	}
	
	private void setComponentProperties()
	{
		scrollList.setViewportView(list);
		scrollIntro.setViewportView(textArea);
		
		scrollList.setPreferredSize(new Dimension(150, 365));
		scrollIntro.setPreferredSize(new Dimension(430, 365));
		
		
		textArea.setFont(new Font("Calibri", 0, 20));
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		
		list.setFont(textArea.getFont());
		
		
		list.setModel(new AbstractListModel()
		{
			String[] item = {"Soft Intro", "How to use"};

			@Override
			public int getSize()
			{
				return item.length;
			}

			@Override
			public Object getElementAt(int index)
			{
				return item[index];
			}
			
		});
		list.setSelectedIndex(0);
	}

	/**
	 * This method will set event for all your component when you press any key
	 */
	private void keyEvent()
	{
		list.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent evt)
			{
				if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					escapeEvent();
			}
		});
		
		textArea.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent evt)
			{
				if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					escapeEvent();
			}
		});
	}
	
	/**
	 * This method is called when Escape key pressed
	 */
	private void escapeEvent()
	{
		dispose();
	}

	/**
	 * This method will set action event to your list when you choose an item
	 */
	private void setListEvent()
	{
		list.addListSelectionListener(new ListSelectionListener()
		{
			
			@Override
			public void valueChanged(ListSelectionEvent e)
			{
				if(list.getSelectedIndex() == 0)
				{
					event.showGameIntro(textArea);
				}
				else if(list.getSelectedIndex() == 1)
				{
					event.showHowToUse(textArea);
					textArea.select(0, 1);
				}
			}
		});
	}
}
